<?php

/**
 * Style loader class
 *
 * @package OWL
 * @subpackage Core
 * @author trung
 * @since 1.5
 */
class Owl_Style {
    var $registered_style = array();
    var $do = array();
    var $done = array();
    var $concat_ver = '';
    var $concat_str = '';
    var $html_str ='';
    var $do_concat = true;
    var $printed_header = false;
    var $base_url='';
    var $default_dir;    
    function  __construct() {
        register_shutdown_function(array($this,"__destruct"));
        $this->Owl_Style();
    }
    function  __destruct() {
        return false;
    }
    function Owl_Style(){
        $this->base_url = SITE_URL;
    }
    function register_style($handle,$src,$ver=false,$media=false,$condition=false){
        $style = array();
        if (empty($handle) || empty ($src))
            return false;
        if (isset ($this->registered_style[$handle]))
            return false;
        $style['src'] = $src;
        $style['ver'] = $ver ? $ver : '1.0';
        $style['media'] = $media ? $media : 'all';
        $style['condition'] = $condition;
        $this->registered_style[$handle]=$style;
        return true;
    }
    
    function print_head_style(){
        $zip = $this->do_concat ? 1 : 0;
	if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
		$zip = 'gzip';
        $this->do_items();
        if (!empty($this->concat_str)){
            $ver = md5($this->concat_ver);
            $href = $this->base_url . "load-styles.php?c={$zip}&load=" . trim($this->concat_str, ', ') . "&ver=$ver";
            echo "<link rel='stylesheet' href='" . $href . "' type='text/css' media='all' />\n";

        }
        if ( !empty($this->html_str) )
            echo $this->html_str;
        $this->concat_str=$this->concat_ver=$this->html_str='';
        $this->printed_header=true;
        return $this->done;
    }
    function do_items($handles=false){
        foreach( $this->do as $key => $handle ) {
			if ( !in_array($handle, $this->done) && isset($this->registered_style[$handle]) ) {
				if ( ! $this->registered_style[$handle]['src'] ) {
					$this->done[] = $handle;
					continue;
				}
				if ( $this->do_item( $handle) )
					$this->done[] = $handle;
				unset( $this->do[$key] );
			}
		}
    }
    function do_item($handle){
        $ver = $this->registered_style[$handle]['ver'];
        $media = $this->registered_style[$handle]['media'];
        $condition = $this->registered_style[$handle]['condition'];
        $src = $this->registered_style[$handle]['src'];
        if ( $this->do_concat ){
		if ( $this->in_default_dir($src) && ($condition===false) && ($media==='all') ) {
				$this->concat_str .= "$handle,";
				$this->concat_ver .= "$handle.$ver";
				return true;
		}
	}
        $href = $this->_css_href( $src, $ver, $handle );
        $end_cond = $tag = '';
	if ( $condition != false ) {
		$tag .= "<!--[if {$condition}]>\n";
		$end_cond = "<![endif]-->\n";
	}
        $tag .= "<link rel='stylesheet' id='$handle-css' href='$href' type='text/css' media='$media' />\n";
        $tag .= $end_cond;
	if ( $this->do_concat )
		$this->html_str .= $tag;
	else
		echo $tag;
    }
    function print_css($handle){
        $style = $this->registered_style[$handle];
        $path =  APS_PATH. $style['src'];
        $content = $this->get_file($path) . "\n";
        $content = preg_replace('/url\s*\(\s*["\']?([^"\'\)]+)["\']?\)/i', 'url("' . SITE_URL . '${1}")', $content);
        echo "<style type='text/css' id='$handle-css'>\n";
        echo "/* <![CDATA[ */\n";
        echo $content;
        echo "/* ]]> */\n";
        echo "</style>\n";
    }
    function get_file($path) {
	if ( function_exists('realpath') )
		$path = realpath($path);
	if ( ! $path || ! @is_file($path) )
		return '';
	return @file_get_contents($path);
    }
    function unregister_style( $handles ) {
            $handle_list = explode(',', $handles);
            foreach ( (array) $handle_list as $handle ){
		unset($this->registered_style[$handle]);
            }
    }

    function enqueue( $handles ) {
                $handle_list = explode(',', $handles);
                if ($this->printed_header){
                    foreach ( (array) $handle_list as $handle ) {
                        if (!in_array($handle, $this->done)){
                            $this->print_css($handle);
                            $this->done[] = $handle;
                        }
                    }
                }else {
                    foreach ( (array) $handle_list as $handle ) {
                    if ( !in_array($handle, $this->do) && isset($this->registered_style[$handle]) ) {
                            $this->do[] = $handle;
                        }
                    }
                }
    }

    function dequeue( $handles ) {
	$handle_list = explode(',', $handles);
        foreach ( (array) $handle_list as $handle ) {
            unset( $this->do[$handle] );
        }
    }
    function _css_href( $src, $ver, $handle ) {
		if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
			$src = $this->base_url . $src;
		}
		//$src = add_query_arg('ver', $ver, $src);
		//return esc_url( $src );
                return  $src;
	}
    function in_default_dir($src) {
		if ( !$this->default_dirs )
			return true;
		foreach ( (array) $this->default_dirs as $test ) {
			if ( 0 === strpos($src, $test) )
				return true;
		}
		return false;
	}


}
// API
function register_style($handle,$src,$ver=false,$media=false,$condition=false){
    global $style_control;
    return $style_control->register_style($handle,$src,$ver,$media,$condition);
}
function enqueue_style( $handles ){
     global $style_control;
     return $style_control->enqueue($handles);
}
function dequeue_style( $handles ){
    global $style_control;
    return $style_control->dequeue($handles);
}
function unregister_style( $handles ) {
     global $style_control;
     return $style_control->unregister_style($handles);
}
function print_style(){
    global $style_control;
    return $style_control->print_head_style();
}
?>